package de.fhl.view;

/**
 * Ueberschrift:  OnlineWahl
 * Copyright:     Copyright (c) 2010 
 * Organisation:  FH Luebeck - Virtuelle FH
 */

import de.fhl.controller.*;
import de.fhl.model.*;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 * Die Klasse LoginView ist die View des Logins über die GUI<br>
 * 
 * @author Achim Krumrein (krumreia@stud.fh-luebeck.de)
 * @author Thomas Weber (webertho@googlemail.de)
 * @version 1.0
 */
public class LoginView extends JFrame implements Observer
{	// MVC Instanzen
	private LoginController	controller;
	private LoginModel		model;
 
	// weitere lokale Variablen...
	private static final long serialVersionUID = 1L;	
	private JButton button;
	private JLabel loginIdLabel;
	private JTextField loginIdField;
	private JLabel nameLabel;
	private JTextField nameField;
	private JLabel ausweisLabel;
	private JTextField ausweisField;
	private JButton loginButton;
	private JButton closeButton;
 
	// Initialisierung Konstruktor
    public LoginView(LoginModel model)
    {
    	this.model = model;
    	this.model.addObserver(this);
    	this.controller = new LoginController(this,model);
    	initGUI();
    }
 
    // Initialisierung der Frame und GUI Elemente
    private void initGUI()
    {
    	// Frame (extends JFrame)
		setTitle("Online Wahl 2010");
		setLayout(new FlowLayout());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(400, 400);
		setResizable(false);
		setVisible(true);
		
 
	    // Andere GUI Elemente...
			nameField = new JTextField(20);
			nameLabel = new JLabel("Name:");
			nameLabel.setLabelFor(nameField);
			ausweisField = new JTextField(20);
			ausweisLabel = new JLabel("Ausweis-Nr:");
			ausweisLabel.setLabelFor(ausweisField);
			loginIdField = new JTextField(10);
			loginIdLabel = new JLabel("Login-ID:");
			loginIdLabel.setLabelFor(loginIdField);

			JPanel fieldPane = new JPanel();
			fieldPane.setLayout(new GridLayout(3, 1));
			fieldPane.add(nameLabel);
			fieldPane.add(nameField);
			fieldPane.add(ausweisLabel);
			fieldPane.add(ausweisField);
			fieldPane.add(loginIdLabel);
			fieldPane.add(loginIdField);

			loginButton = new JButton("Anmelden");
	        loginButton.setActionCommand("fromLoginButton");
	        loginButton.addActionListener(controller);
			closeButton = new JButton("Schliessen");
			closeButton.setActionCommand("fromCloseButton");
			closeButton.addActionListener(controller);

			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new GridLayout(2, 1));
			buttonPane.add(loginButton);
			buttonPane.add(closeButton);
			
			Container con = getContentPane();
			con.add(fieldPane);
			con.add(buttonPane);

			pack();
    }
 

 
    // Setter fuer den Buttontext
    public void setButtonText(String text)
    {
    	//this.button.setText(text);
    	//this.loginButton.setText(text);
    	this.closeButton.setText(text);

    }
 
    @Override
    // Event-Handling
    public void update(Observable arg0, Object eventCommand)
    {
        if (eventCommand.equals("done"))
        {
        	if(model.getButtonState())
        		setButtonText("I'm happy :)");
        	else
        		setButtonText("I'm sad :(");
        }
    }
}
